package com.four.seckill.web.controller;

import com.four.common.utils.R;
import com.four.seckill.util.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


/**
 * @Author: hxw
 * @Date: 2022/01/07/14:11
 * @Description
 */
@ControllerAdvice
public class WebController {

    private static final Logger logger = LoggerFactory.getLogger(WebController.class);
    /**
     * 捕捉全局异常
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public String errorHandler(Exception ex) {

        logger.error(ex.getMessage(),ex);

        return "对不起您的网络错误，请稍后再试";
    }

    /**
     * 捕获自定义异常
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = MyException.class)
    public R myErrorHandler(MyException ex) {

        return R.error(ex.getCode(),ex.getGetMsg());
    }
}
